<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <!-- jq -->
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 模板引擎 -->
    <script src="./libs/template-web.js"></script>
        <!-- 导入日期插件 -->
        <script src="./libs/jedate/js/jedate.js"></script>
        <link rel="stylesheet" href="./libs/jedate/css/jeDate-test.css">
        <link rel="stylesheet" href="./libs/jedate/css/jedate.css">
        <!-- 导入富文本插件 -->
        <script src="./libs/wangEditor/wangEditor.min.js"></script>
        <script src="./libs/https.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章编辑
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control title" id="inputTitle" value="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img src="images/pic06.jpg" class="article_cover">
                        <input name="cover" type="file" id="inputCover">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCategory" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select class="form-control category" name="categoryId">
                            <option>类别一</option>
                            <option>类别二</option>
                            <option>类别三</option>
                            <option selected>类别四</option>
                            <option>类别五</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <!-- 日期控件容器 -->
                        <div class="jeinpbox">
                            <input type="text" class="jeinput" id="testico" placeholder="YYYY-MM-DD" name="date">
                            <div class="icons jebtns"></div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <div id="editCom">
                            <!-- <p>欢迎使用<b>wangEditor</b>富文本编辑器</p> -->
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success btn-edit">修改</button>
                        <button type="submit" class="btn btn-default btn-draft">存为草稿</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>

</html>
<script>
    $(function () {
        // 获得需要编辑的文章id
        let articleId = window.location.search.split('=')[1];
        // console.log(articleId);

        // 1.图片预览
        $('#inputCover').on('change', function () {
            let picData = this.files[0];
            let picUrl = URL.createObjectURL(picData);
            $('.article_cover').attr('src', picUrl);
        })

        // 2.获取文章类别，渲染
        $.ajax({
            type: 'get',
            url: BigNew.category_list,
            success: (res) => {
                if (res.code == 200) {
                    // 3.结合数据 渲染结构
                    let htmlStr = '';
                    $.each(res.data, (index, value) => {
                        htmlStr += `
                        <option value='${value.id}''>${value.name}</option>
                        `
                    })
                    $('.category').empty().html(htmlStr);
                }
            }
        })

        // 4. 调用日期插件启动方法
        $('.jebtns').on('click', function () {
            jeDate(
                '#testico',
                {
                    trigger: false,
                    format: 'YYYY-MM-DD',
                    // theme:{bgcolor:'#D91600',pnColor:'#FF6653'}
                    zIndex: 100001
                }
            )
        })

        // 5. 调用富文本插件代码
        const E = window.wangEditor;
        const editor = new E('#editCom');
        editor.create();

        // 6. 根据文章id获得指定的文章数据，并且渲染结构
        $.ajax({
            type:'get',
            url:BigNew.article_search,
            data:{
                id:articleId
            },
            success:(res)=>{
                if(res.code ==200){
                    // 7. 将数据渲染到指定的结构
                    $('#inputTitle').val(res.data.title);
                    $('.article_cover').attr('src',res.data.cover);
                    $('.category').val(res.data.categoryId);
                    $('#testico').val(res.data.date);
                    editor.txt.html(res.data.content)
                }
            }
        })

        // 8. 封装函数，保存文章数据
        function saveData(state){
            // 9. 获得当前文章的数据，利用formDat啊对象
            let fd = new FormData(document.querySelector('form'));

            // 10手动追加fd数据
            fd.append('id',articleId);
            fd.append('content',editor.txt.html());
            fd.append('state',state)

            // 11 发起ajax请求，修改文章数据
            $.ajax({
                type:'post',
                url:BigNew.article_edit,
                data:fd,
                contentType:false,
                processData:false,
                success:(res)=>{
                    if(res.code == 200){
                        alert('文章修改成功');
                        window.location = './article_list.html'
                    }
                }
            })
        }

        $('.btn-edit').on('click',(e)=>{
            e.preventDefault();
            saveData('已发布');
        })
        $('.btn-draft').on('click',(e)=>{
            e.preventDefault();
            saveData('草稿');
        })

    })
</script>